package com.tj.demo.util.toolkit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

/**
 * @ClassName: TimeUtil
 * @Author: TanJun
 * @Date: 2021/5/15 17:25
 * @Description:
 * @Version: 1.0
 * @Copyright:
 *
 * 1.常见的时间配置工具包
 *      1.getTime：时间加减处理
 *      2.getDateList  得到日期段之间的集合
 * 2.java8常见Map、List操作
 *      1.distinctByKey
 */
public class FuncUtil {
    /***
     * 
     * @author 谭军(tanjun@bosssoft.com.cn)
     * @Description dsad
     * @Date 2022/7/14 0:40
     * @Param 
     * @return 
     */
    static Logger logger = LoggerFactory.getLogger(FuncUtil.class);
    static Logger logger1 = LoggerFactory.getLogger(FuncUtil.class);

    /**
     * 时间加减格式
     * @param dataTime  时间字符串 测试1
     * @param num       历史时长
     * @param sdf       对应格式字符时间
     * @param type      类型 1：加减小时 2：加减天数  3：加减年数据 4:加减分钟数
     * @return
     */
    public static String getTime(String dataTime, int num, SimpleDateFormat sdf, Integer type) {
        String dataTimeConfig = null;
        //SimpleDateFormat sdf = new SimpleDateFormat(sdfString);
        try {
            Date d1 = sdf.parse(dataTime);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(d1);
            switch (type){
                case 1:
                    calendar.add(Calendar.HOUR, -num);
                    break;
                case 2:
                    calendar.add(Calendar.DAY_OF_MONTH, -num);
                    break;
                case 3:
                    calendar.add(Calendar.YEAR, -num);
                    break;
                case 4:
                    calendar.add(Calendar.MINUTE, -num);
                    break;
            }
            dataTimeConfig = sdf.format(calendar.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dataTimeConfig;
    }

    /**
     * 获取日期段之间所有日期
     * @param start 开始时间  yyyy-MM-dd
     * @param end   结束时间  yyyy-MM-dd
     * @return List<yyyy-MM-dd>
     * @throws Exception
     */
    public static List<String> getDateList(String start, String end) {
        List<String> list = new ArrayList<>();
        LocalDate startDate = LocalDate.parse(start);
        LocalDate endDate = LocalDate.parse(end);
        long distance = ChronoUnit.DAYS.between(startDate, endDate);
        if (distance < 1) {
            return list;
        }
        Stream.iterate(startDate, d -> d.plusDays(1)).limit(distance + 1).forEach(f -> list.add(f.toString()));
        return list;
    }

    /**
     * 按照filter方式对list去重
     * @param keyExtractor 这里需要注意 先排好序
     * @param <T>
     * @return
     */
    public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }
}
